package cn.com.headfree.util;

import cn.com.headfree.entity.TDeclareEvaluationInfo;
import cn.com.headfree.entity.TDeclareExpertInfo;
import cn.com.headfree.entity.TPersonYearPointDetail;
import cn.com.headfree.entity.TSalaryInfo;
import cn.com.headfree.vo.TDeclareEvaluationPointVo;
import cn.com.headfree.vo.TDeclareExpertPointVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelExportUtil {

    private static Logger log = LogManager.getLogger(ExcelExportUtil.class);

    private ExcelExportUtil() {
    }

    /**
     * 生成sheet并导入内容 传入工作簿，创建sheet
     * @param workBook
     * @param headers
     * @param content
     * @param sheetName
     * @return
     */
    public static XSSFWorkbook exportExcel(XSSFWorkbook workBook, String[] headers, String[][] content, String sheetName) {

        XSSFCellStyle cellStyle = workBook.createCellStyle();
        //设置标题单元格样式
        XSSFCellStyle cellStyleHeader = workBook.createCellStyle();
        cellStyleHeader.setAlignment(HorizontalAlignment.CENTER);
        cellStyleHeader.setVerticalAlignment(VerticalAlignment.CENTER);

        cellStyleHeader.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyleHeader.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyleHeader.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyleHeader.setBorderRight(BorderStyle.THIN);// 右边框
        cellStyleHeader.setFillForegroundColor(new XSSFColor(new java.awt.Color(156, 195, 230)));//设置背景色
        cellStyleHeader.setFillPattern(FillPatternType.ALT_BARS);//填充模式
        //水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyle.setBorderRight(BorderStyle.THIN);// 右边框

        XSSFSheet sheet = workBook.createSheet(sheetName);
        sheet.setVerticallyCenter(true);
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell(i);
            if(i == 0){
                sheet.setColumnWidth(i, 25 * 256);//设置宽度为30字节
            }else if(i == 3 || i == 5 || i == 10 || i == 11 || i == 14){
                sheet.setColumnWidth(i, 14 * 256);//设置宽度为10字节
            }else{
                sheet.setColumnWidth(i, 10 * 256);//设置宽度为10字节
            }
            cell.setCellValue(headers[i]);
            cell.setCellStyle(cellStyleHeader);
        }
        if(content != null){
            for (int i = 0; i < content.length; i++) {
                row = sheet.createRow((int) i + 1);//从第二行开始添加内容
                //写入数据
                if(content != null){
                    for (int j = 0; j < content[i].length; j++) {
                        XSSFCell cell2 = row.createCell(j);//在第一列创建表格
                        cell2.setCellValue(content[i][j]);
                        cell2.setCellStyle(cellStyle);
                    }
                }
            }
        }
        return workBook;
    }


    /**
     * 组装数据：薪档申报查询
     */
    public static String[][] declareExpertInfoQuerHead(List<TDeclareExpertInfo> list, String[] titles) {
        String[][] content = null;
        content = new String[list.size()][titles.length];
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                content[i][0] = list.get(i).getErpNo();
                content[i][1] = list.get(i).getUserName();
                if(list.get(i).getYear() != null && list.get(i).getYear() != 0){
                    content[i][2] = list.get(i).getYear().toString();
                }
                content[i][3] = list.get(i).getDeptName();
                if(list.get(i).getAuditStatus() != null){
                    content[i][4] = list.get(i).getAuditStatus().toString();
                }
            }
        }
        return content;
    }

    /**
     * 组装数据
     * @return personYearPointDetailHead=部门,姓名,ERP编号,学历,专业技术资格,技能等级,工龄,人才称号,学术成果,发明专利,管理科技创新,成果推广应用,竞赛考试,个人荣誉,上年度剩余积分,本年度积分,合计,调整后合计
     */
    public static String[][] makePersonYearPointDetailHead(List<TDeclareExpertPointVo> list, String[] titles) {
        String[][] content = null;
        content = new String[list.size()][titles.length];
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                content[i][0] = list.get(i).getDeptName();
                content[i][1] = list.get(i).getUserName();
                content[i][2] = list.get(i).getErpNo();
                if(list.get(i).getYear() != null){
                    content[i][3] = list.get(i).getYear().toString();
                }
                if(list.get(i).getYearEffectPoint() != null){
                    content[i][4] = list.get(i).getYearEffectPoint().toString();
                }
                if(list.get(i).getContiStudyPoint() != null){
                    content[i][5] = list.get(i).getContiStudyPoint().toString();
                }
                if(list.get(i).getSdtTutorPrentPoint() != null){
                    content[i][6] = list.get(i).getSdtTutorPrentPoint().toString();
                }
                if(list.get(i).getHonorPoint() != null){
                    content[i][7] = list.get(i).getHonorPoint().toString();
                }
                if(list.get(i).getTecManPoint() != null){
                    content[i][8] = list.get(i).getTecManPoint().toString();
                }
                if(list.get(i).getTecInnovPoint() != null){
                    content[i][9] = list.get(i).getTecInnovPoint().toString();
                }
                if(list.get(i).getPatentPoint() != null){
                    content[i][10] = list.get(i).getPatentPoint().toString();
                }
                if(list.get(i).getAcadePoint() != null){
                    content[i][11] = list.get(i).getAcadePoint().toString();
                }
                if(list.get(i).getStandPoint() != null){
                    content[i][12] = list.get(i).getStandPoint().toString();
                }
                if(list.get(i).getTecStandardPoint() != null){
                    content[i][13] = list.get(i).getTecStandardPoint().toString();
                }
                if(list.get(i).getExamPoint() != null){
                    content[i][14] = list.get(i).getExamPoint().toString();
                }
                if(list.get(i).getInterviewPoint() != null){
                    content[i][15] = list.get(i).getInterviewPoint().toString();
                }
                if(list.get(i).getTotalPoint() != null){
                    content[i][16] = list.get(i).getTotalPoint().toString();
                }
            }
        }
        return content;
    }


    /**
     * 专家人才评价
     * @param list
     * @param titles
     * @return
     */
    public static String[][] declareEvaluationInfoQuerHead(List<TDeclareEvaluationInfo> list, String[] titles) {
        String[][] content = null;
        content = new String[list.size()][titles.length];
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                content[i][0] = list.get(i).getErpNo();
                content[i][1] = list.get(i).getUserName();
                if(list.get(i).getYear() != null && list.get(i).getYear() != 0){
                    content[i][2] = list.get(i).getYear().toString();
                }
                content[i][3] = list.get(i).getDeptName();
                if(list.get(i).getAuditStatus() != null){
                    content[i][4] = list.get(i).getAuditStatus().toString();
                }
            }
        }
        return content;
    }

    /**
     * 组装数据
     * @return personYearPointDetailHead=部门,姓名,ERP编号,学历,专业技术资格,技能等级,工龄,人才称号,学术成果,发明专利,管理科技创新,成果推广应用,竞赛考试,个人荣誉,上年度剩余积分,本年度积分,合计,调整后合计
     */
    public static String[][] makePersonYearEvaluationPointDetailHead(List<TDeclareEvaluationPointVo> list, String[] titles) {
        String[][] content = null;
        content = new String[list.size()][titles.length];
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                content[i][0] = list.get(i).getDeptName();
                content[i][1] = list.get(i).getUserName();
                content[i][2] = list.get(i).getErpNo();
                if(list.get(i).getYear() != null){
                    content[i][3] = list.get(i).getYear().toString();
                }
                if(list.get(i).getYearEffectPoint() != null){
                    content[i][4] = list.get(i).getYearEffectPoint().toString();
                }
                if(list.get(i).getContiStudyPoint() != null){
                    content[i][5] = list.get(i).getContiStudyPoint().toString();
                }
                if(list.get(i).getSdtTutorPrentPoint() != null){
                    content[i][6] = list.get(i).getSdtTutorPrentPoint().toString();
                }
                if(list.get(i).getHonorPoint() != null){
                    content[i][7] = list.get(i).getHonorPoint().toString();
                }
                if(list.get(i).getTecManPoint() != null){
                    content[i][8] = list.get(i).getTecManPoint().toString();
                }
                if(list.get(i).getTecInnovPoint() != null){
                    content[i][9] = list.get(i).getTecInnovPoint().toString();
                }
                if(list.get(i).getPatentPoint() != null){
                    content[i][10] = list.get(i).getPatentPoint().toString();
                }
                if(list.get(i).getAcadePoint() != null){
                    content[i][11] = list.get(i).getAcadePoint().toString();
                }
                if(list.get(i).getStandPoint() != null){
                    content[i][12] = list.get(i).getStandPoint().toString();
                }
                if(list.get(i).getTecStandardPoint() != null){
                    content[i][13] = list.get(i).getTecStandardPoint().toString();
                }
//                if(list.get(i).getExamPoint() != null){
//                    content[i][14] = list.get(i).getExamPoint().toString();
//                }
//                if(list.get(i).getInterviewPoint() != null){
//                    content[i][15] = list.get(i).getInterviewPoint().toString();
//                }
                if(list.get(i).getTotalPoint() != null){
                    content[i][14] = list.get(i).getTotalPoint().toString();
                }
            }
        }
        return content;
    }

    /**
     *
     * 向页面导出excel
     *
     * @param response
     * @param fileName 文件名称
     * @param workbook 工作簿
     */
    public static void exportExcel2Browser(HttpServletResponse response, String fileName, XSSFWorkbook workbook) {
        OutputStream os = null;
        try {
            // 解决文件乱码
            fileName = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", "%20");// 其他浏览器
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            os = response.getOutputStream();
            workbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
